package com.fire.control.system.module.common.controller;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import com.fire.control.system.config.redis.RedisService;
import com.fire.control.system.dto.UserDTO;
import com.fire.control.system.module.user.service.IUserService;
import com.fire.control.system.statics.Key;

public class BaseController extends MultiActionController {
	
	private static final Logger logger = LoggerFactory.getLogger(BaseController.class);
	private static final ThreadLocal<HttpServletRequest> requestContainer = new ThreadLocal<HttpServletRequest>();
	private static final ThreadLocal<HttpServletResponse> responseContainer = new ThreadLocal<HttpServletResponse>();
	protected HttpSession session;
	@Autowired RedisService redisService;
	@Autowired IUserService userService;
	
	@ModelAttribute
	protected void inItParam(HttpServletRequest request,HttpServletResponse response){
		requestContainer.set(request);
		responseContainer.set(response);
        session = getRequest().getSession(true);
	}
	
	/**
	 * 获取当前线程的request对象
	 * @Description: TODO
	 * @param @return   
	 * @return HttpServletRequest  
	 * @throws
	 * @author wanbo
	 * @date 2016年11月25日
	 */
	protected final HttpServletRequest getRequest() {
        return requestContainer.get();
    }
	
	/**
	 * 获取当前线程的response对象
	 * @Description: TODO
	 * @param @return   
	 * @return HttpServletResponse  
	 * @throws
	 * @author wanbo
	 * @date 2016年11月25日
	 */
    protected final HttpServletResponse getResponse() {
        return responseContainer.get();
    }
    
    public void setSessionAttribute(String key,Object object){
    	session.setAttribute(key,object);
    }
    
    public UserDTO getUser(){
    	String userId = getRequest().getHeader(Key.userId.toString());
    	String authorization = getRequest().getHeader(Key.authorization.toString());
    	if(StringUtils.isNotEmpty(userId) && StringUtils.isNotEmpty(authorization)){
    		return userService.getUserInfo(userId);
    	}else{
    		return (UserDTO) session.getAttribute(Key.session_key_lora_user_id.toString());
    	}
    }
	
	
}
